package ch.zhaw.mini.doclet.formater;


public abstract class FormatPage {
	/**
	 * Default Newline Charakter to be used
	 */
	static final String NEWLINE = "\n";

	/**
	 * Default Marker for cutted text to be used
	 */
	static final String POINTS = " ... ";

	/**
	 * Standard Index Page String Delimiter
	 */
	static String DELIMITER = ",";
	



	/**
	 * Wrapper Method for all Comment Strings to filter the Text before Posting
	 * it to the Page
	 * 
	 * @param rawComment
	 *            The whole Comment to be printed
	 * @return Filtered Comment String
	 */
	protected StringBuilder createComment(StringBuilder rawComment) {
		return new StringBuilder(rawComment.toString().replaceAll("\n +", " "));
	}

	/**
	 * Wrapper Method to cut down the Comment Strings to 300 Chars length
	 * maximal. If there is a .\n or \n\n within the first 200 Chars or a \n
	 * within the first 300 chars, it will be cutted at this location instead
	 * 
	 * @param rawComment
	 *            The whole Comment to be printed
	 * @return Cutted Comment String with 300 chars lengt maximal
	 */
	protected StringBuilder createSmallComment(StringBuilder rawComment) {
		StringBuilder output = new StringBuilder();
		rawComment= createComment(rawComment);

		if (rawComment.length() > 200) {

			int cutpos = 200;
			boolean addpoints = true;

			// Check first .\n string
			if (0 < rawComment.indexOf("." + NEWLINE) && rawComment.indexOf("." + NEWLINE) < 200) {
				cutpos = rawComment.indexOf("." + NEWLINE) + 1;
				addpoints = false;

			}
			// Check first \n\n string
			else if (0 < rawComment.indexOf(NEWLINE + NEWLINE) && rawComment.indexOf(NEWLINE + NEWLINE) < 200) {
				cutpos = rawComment.indexOf(NEWLINE + NEWLINE);
				addpoints = false;

			}
			// Check first \n string
			else if (0 < rawComment.indexOf(NEWLINE) && rawComment.indexOf(NEWLINE) < 300) {
				cutpos = rawComment.indexOf(NEWLINE);
				addpoints = true;

			}
			output.append( rawComment.substring(0, cutpos));
			if (addpoints)
				output.append(POINTS);

			

		} else {

			output.append(rawComment);


		}
		// TODO: Remove doubled Spaces and check if NEWLINE Chars are
		// Handled corrctly
		output = new StringBuilder( output.toString().replace(NEWLINE, " "));
		output= new StringBuilder(output.toString().replace("  ", " "));
		output= new StringBuilder(output.toString().replace("  ", " "));

		return output;
	}

}
